/**
* @Title: queryInstitution_list_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-xieqi001
* @date 2014年11月10日 下午2:38:47
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('queryInstitutionListCtrl', ['$scope','InsuranceService','$stateParams','$modalInstance','CommunityService','doctorInfoService','CheckRows','Modal','$http','BaseURL',
	                                function($scope,InsuranceService,$stateParams,$modalInstance,CommunityService,doctorInfoService,CheckRows,Modal,$http,baseUrl){
       $scope.form = {};
       $scope.form1 = {};
       
       //add  by xf
       //机构
       function initMedicalOrgInfoList (){
           InsuranceService.initMedicalOrgInfoListWithLevel().success(function(response){
               $scope.medicalOrgInfoList = response.medicalOrgList;
           });
       }
       
       //监听机构分类值
       $scope.$watch('form.orgType',function(){
           initMedicalOrgInfoList();
       });
       
       $scope.$watch('form.medicalOrgInfo',function(newValue,oldValue,scope){
           if(oldValue != newValue){
               try{
                   newValue = JSON.parse(newValue);    
               }catch(e){}
               
               if(newValue)
               {
                   $scope.form.id = newValue.id;
                   $scope.form.cityCode = newValue.cityCode;
                   $scope.form.medicalOrgName = newValue.medicalOrgName;
                   $scope.form.medicalOrgCode = newValue.medicalOrgCode;
                   $scope.form.medicalOrgLevel = newValue.medicalOrgLevel;
                   $scope.form.fullAddress = newValue.fullAddress;
                   
                   getprovincialInfoByHosCode(newValue.medicalOrgCode);
                   $scope.form1 = {};
                   $scope.cityDepList = [];
               }
               else
               {
                   $scope.form = {};
                   $scope.form1 = {};
                   $scope.cityDepList = [];
               }
           }
       });
       
       $scope.$watch('form1.cityDepInfo',function(newValue,oldValue,scope){
           if(oldValue != newValue){
               try{
                   newValue = JSON.parse(newValue);    
               }catch(e){}
               
               if(newValue){
                   $scope.form1.cityDepName = newValue.cityDepName;
                   $scope.form1.cityDepCode = newValue.cityCode;
                   $scope.form1.provincialName = newValue.codeName;
                   $scope.form1.provincialCode = newValue.codeNO;
               }
               else{
                   $scope.form1 = {};
               }
           }
       });
       
       function getprovincialInfoByHosCode(medicalOrgCode){
           if(!medicalOrgCode){
               return false;
           }
           
           $http({
               method: "get",
               url: baseUrl+'community/queryProvincialInfoByHosCode.do',
               params:{
                   hosCode:medicalOrgCode
               }
          }).success(function(response, status, headers, config){
              $scope.cityDepList = response;
          });
       }
       
       
        //重置
        $scope.onReset = function() {
            $scope.form = {};
            $scope.form1 = {};
        };

		
        //关闭model
		 $scope.onClose = function() {
			$modalInstance.dismiss();
		 }
		
	      //确定事件
         $scope.add = function() {
             if(!$scope.form.id){
                 Modal.alert("请选择医疗机构!!!"); 
                 return false;
             }
             if(!$scope.form1.cityDepCode){
                 Modal.alert("请选择医院科室!!!"); 
                 return false;
             }
             
           var dataRows = $scope.doctorInfoDetail.checkInstitutionRows;
           for(var i = 0; i<dataRows.length; i++){
               if($scope.form.medicalOrgCode ==dataRows[i].providerCode&&$scope.form1.cityDepCode == dataRows[i].departNo){
                   Modal.alert("医院科室不能重复！");
                   return false;
               }
           }
             
             //{"fullAddress":$scope.form.fullAddress,"hospitalLevel":$scope.form.medicalOrgLevel,"orgType":"医院","providerCode" :$scope.form.medicalOrgCode,"providerName":$scope.form.medicalOrgName,"providerId":$scope.form.id}
             //$scope.$parent.addData();
                    $modalInstance.close({
                        "fullAddress": $scope.form.fullAddress,
                        "hospitalLevel": $scope.form.medicalOrgLevel,
                        "orgType": "医院",
                        "providerCode": $scope.form.medicalOrgCode,
                        "providerName": $scope.form.medicalOrgName,
                        "providerId": $scope.form.id,
                        "departName":$scope.form1.cityDepName,
                        "departNo":$scope.form1.cityDepCode
                        
                    });
         }
       
    }]);
})();